package cn.iocoder.yudao.module.handguard.controller.app.products;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.handguard.controller.admin.journalism.vo.JournalismRespVO;
import cn.iocoder.yudao.module.handguard.controller.admin.product.vo.ProductRespVO;
import cn.iocoder.yudao.module.handguard.convert.journalism.JournalismConvert;
import cn.iocoder.yudao.module.handguard.convert.product.ProductConvert;
import cn.iocoder.yudao.module.handguard.dal.dataobject.journalism.JournalismDO;
import cn.iocoder.yudao.module.handguard.dal.dataobject.product.ProductDO;
import cn.iocoder.yudao.module.handguard.dal.dataobject.publicitytype.PublicityTypeDO;
import cn.iocoder.yudao.module.handguard.dal.mysql.publicitytype.PublicityTypeMapper;
import cn.iocoder.yudao.module.handguard.service.product.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * @author: liuweijun
 * @since: 2023/9/13 14:12
 * @description:
 */

@Tag(name = "小程序 - 金融产品")
@RestController
@RequestMapping("/mini/products")
@Validated
public class ProductsController {


    @Resource
    private ProductService productService;

    @Resource
    private PublicityTypeMapper publicityTypeMapper;

    @GetMapping("/list")
    @Operation(summary = "产品列表")
    @PermitAll
    public CommonResult<List<ProductRespVO>> getProductList() {
        List<ProductDO> list = productService.getProductList();
        return success(ProductConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/getFontByKey")
    @Operation(summary = "获得产品字眼")
    @PermitAll
    public CommonResult<String> getWordingByKey(@RequestParam("key") String  key) {
        String product = productService.getWordingByKey(key);
        return success(product);
    }


    @GetMapping("/get")
    @Operation(summary = "产品详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PermitAll
    public CommonResult<ProductRespVO> getProduct(@RequestParam("id") Long id) {
        ProductDO product = productService.getProduct(id);
        PublicityTypeDO publicityTypeDO = publicityTypeMapper.selectById(product.getProductType());
        ProductRespVO respVO = ProductConvert.INSTANCE.convert(product);
        if (publicityTypeDO!=null){
            respVO.setProductTypeName(publicityTypeDO.getTypeNames());
        }
        return success(respVO);
    }


}
